// Copyright (c) 2020 Presto Labs Pte. Ltd.
// Author: jing

#pragma once

#include <map>
#include <string>
#include <vector>

#include <jwt-cpp/jwt.h>
#include "coin2/exchange/base/feed/processor.h"

namespace coin2::exchange::quoinex::feed_v2 {
namespace impl {

static const std::string kAccessKey = "1075952";
static const std::string kSecretKey
  = "1bVfUOTPU7IdTfzxuDQSXZiv50budWwBpD3o4ydJfly1b0y+IGrIbbY6gIgbu5q6PENQ8hsZ+rVv+6IyvX1dUw==";

class QuoinexRequestGenerator : public base::feed::RequestGenerator {
 public:
  virtual std::string GenerateAuthRequest(const std::string& key_filepath) {
    using namespace std::chrono;
    milliseconds ms = duration_cast<milliseconds>(system_clock::now().time_since_epoch());
    auto auth_token = jwt::create()
    .set_type(std::string("JWT"))
    .set_algorithm(std::string("HS256"))
    .set_payload_claim("token_id", jwt::claim(kAccessKey))
    .set_payload_claim("path", jwt::claim(std::string("/realtime")))
    .set_payload_claim("nonce", jwt::claim(std::to_string(ms.count())))
    .sign(jwt::algorithm::hs256{kSecretKey});

    std::string auth_request =
    "{\"event\":\"quoine:auth_request\","
    "\"data\":{\"path\":\"/realtime\",\"headers\":{\"X-Quoine-Auth\":\"{token}\"}}}";

    boost::replace_all(auth_request, "{token}", auth_token);

    return auth_request;
  }
};

}  // namespace impl
using impl::QuoinexRequestGenerator;
}  // namespace coin2::exchange::quoinex::feed_v2
